package controller;

import dao.GantaDao;
import entity.Ganta;
import entity.Roleline_lx;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import service.GantaService;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/ganta")
@Scope("prototype")
public class GantaController {

    @Resource(name = "gantaService")
    private GantaService gantaService;
    @Resource(name = "gantaDao")
    private GantaDao gantaDao;
    @RequestMapping("/queryConditionPage")
    @ResponseBody
    public Map<String,Object> queryConditionPage(String xlid,int state,int pageNumber, int pageSize){

        Map<String,Object> map = new HashMap<String,Object>() ;
        Map<String,Object> map1 = new HashMap<String,Object>() ;
        System.out.println(1);
        int xlid1 = -1;
        System.out.println("2");
        if(xlid != null && xlid != ""){
            xlid1 = Integer.parseInt(xlid);
        }
        System.out.println("4");

        map1.put("xlid",xlid1);
        map1.put("state",state);

        System.out.println("5");

        int i = (pageNumber-1)*pageSize;
        map1.put("11",i);
        map1.put("22",pageSize);
        System.out.println(map1.get(xlid));
        System.out.println(map1.get(state));
        System.out.println(map1.get(11));
        System.out.println(map1.get(22));
        map.put("total",gantaDao.getCount(map1)) ;
        map.put("rows",gantaDao.query(map1));
        return map ;
    }
    @RequestMapping("/add")
    @ResponseBody
    public Map<String,Object> add(String gtid,int nstate,int xlid,HttpServletRequest requset,int pageSize,int pageNumber) {
        Ganta ganta = new Ganta();
        ganta.setGtid(gtid);
        ganta.setState(nstate);
        ganta.setXlid(xlid);
        gantaService.add(ganta);
        Ganta gt  = new Ganta();
        return queryConditionPage(null,-1,pageNumber,pageSize);
    }

    @RequestMapping("/oneGanta")
    @ResponseBody
    public Ganta oneGanta(int id){
        return gantaService.one(id);
    }
    @RequestMapping("/delGanta")
    @ResponseBody
    public Map<String,Object>  delete(int id,HttpServletRequest requset,String xlid,int state,int pageSize,int pageNumber){
        gantaService.del(id);
        Ganta gt  = new Ganta();
        return queryConditionPage(xlid,state,pageNumber,pageSize);
    }
   @RequestMapping("/updGanta")
   @ResponseBody
  public Map<String,Object> update(String gtid,int xgstate,HttpServletRequest requset, int pageNumber, int pageSize){
        Ganta ganta = new Ganta();
        ganta.setGtid(gtid);
        ganta.setState(xgstate);
        gantaService.upd(ganta);
       return queryConditionPage(null,-1,pageNumber,pageSize);  }
}
